﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;


namespace SunshineAirlines
{
    public partial class Login : Form
    {
        int flag = 0;
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //label1.ForeColor = ColorTranslator.FromHtml("#196AA6");
           if(flag==0)
            { label5.Visible = false;
                textBox3.Visible = false;

            }
            string a = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
            Random r = new Random();
            string idc = "";
            for (int i = 0; i < 4; i++)
            {
                idc += a[r.Next(35)];
            }
            label6.Text = idc.ToString();

            if (ConfigurationManager.AppSettings["autologin"].Equals("true"))
            {
                DateTime start = Convert.ToDateTime(ConfigurationManager.AppSettings["last"]);
                DateTime end = DateTime.Now;
                TimeSpan d = end - start;
                if (d.TotalDays <= 7)
                {
                    textBox1.Text = ConfigurationManager.AppSettings["name"];
                    textBox2.Text = ConfigurationManager.AppSettings["password"];
                    //this.Hide();
                    button1_Click(null, null);
                }
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
         
            string sql = "select count(*) from Users where RoleId=1 and Email='" + textBox1.Text.Trim() + "' and Password='" + textBox2.Text.Trim() + "'";
            int result = (int)DBHelper.ExecuteScalar(sql);
            string sql1 = "select count(*) from Users where  RoleId=2 and Email='" + textBox1.Text.Trim() + "' and Password='" + textBox2.Text.Trim() + "'";
            int result1 = (int)DBHelper.ExecuteScalar(sql1);
            if (result > 0 )
            {
                User_Menu f1 = new User_Menu();
                f1.Show();
            }
            if (result1 > 0 )
            {
                Administrator_Menu f2 = new Administrator_Menu();
                f2.Show();
            }
            if (result1 == 0 && result == 0)
            {
                flag+=1;
                MessageBox.Show("password error");
                if (flag == 3)
                {
                    this.textBox3.Visible = true;
                    this.label5.Visible = true;
                }
            }

            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (checkBox1.Checked)
            {
                cfa.AppSettings.Settings["name"].Value = textBox1.Text;
                cfa.AppSettings.Settings["password"].Value =textBox2.Text;
                cfa.AppSettings.Settings["autologin"].Value = "true";
                cfa.AppSettings.Settings["last"].Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {
                cfa.AppSettings.Settings["name"].Value = "";
                cfa.AppSettings.Settings["password"].Value = "";
                cfa.AppSettings.Settings["autologin"].Value = "false";
                cfa.AppSettings.Settings["last"].Value = "";
            }
            cfa.Save();
            //cfa.Save(ConfigurationSaveMode.Modified);
            //ConfigurationManager.RefreshSection("appSettings");

        }

        private void label6_Click(object sender, EventArgs e)
        {
            string a = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
            Random r = new Random();
            string idc = "";
            for (int i = 0; i < 4; i++)
            {
                idc += a[r.Next(35)];
            }
            label6.Text = idc.ToString();

        }
    }
}
